﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TruongVu.Data;

namespace TruongVu.Business
{
    public class News
    {
        public struct NewsModel
        {
            public New NewObject;
            public string ImageFilePath;
        }

        public List<New> GetNews()
        {
            var data = (from n in Singleton.DataContext.News
                        select n).ToList();
            
            return data;
        }

        public New GetNewbyID(int id)
        {
            var n = (from c in Singleton.DataContext.News
                     where c.ID == id
                     select c).SingleOrDefault();
            return n;
        }

        public IList<New> GetNewbyIDList(int id)
        {
            var n = (from c in Singleton.DataContext.News
                     where c.ID == id
                     select c).ToList();
            return n;
        }

        public void InsertNews(New news)
        {
            // insert news
            Singleton.DataContext.News.InsertOnSubmit(news);
            Singleton.DataContext.SubmitChanges();
        }

        public void UpdateNews(New updatedNews)
        {
            var news = (from c in Singleton.DataContext.News
                        where c.ID == updatedNews.ID
                        select c).SingleOrDefault();
            if (news != null)
            {
                news.Date = updatedNews.Date;
                news.Title = updatedNews.Title;
                news.Content = updatedNews.Content;
                news.Image = updatedNews.Image;
                news.Highlight = updatedNews.Highlight;
                
                Singleton.DataContext.SubmitChanges();
            }
        }

        public void DeleteNews(int id)
        {
            var news = (from c in Singleton.DataContext.News
                        where c.ID == id
                        select c).SingleOrDefault();

            if (news != null)
            {
                Singleton.DataContext.News.DeleteOnSubmit(news);
                Singleton.DataContext.SubmitChanges();
            }
        }
    }
}
